<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="交易平台" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="openPlatformId">
              <j-dict-select-tag type="list" v-model="openPlatformId" dictCode="open_platform,platform_name,id" placeholder="请选择开放平台id" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="交易账户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tradingAccount">
              <a-select mode="tags" style="width: 100%" placeholder="交易账户"  v-model="tradingAccount" >
                <a-select-option v-for="(item, index) in accountOption" :key="item.key" :value="item.value">
                  {{ item.value }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="交易品种" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tradingVariety">
              <a-select mode="tags" style="width: 100%" placeholder="交易品种"  v-model="tradingVariety" >
                <a-select-option v-for="(item, index) in varietyOption" :key="item.key" :value="item.value">
                  {{ item.value }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'OrderPlatformForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data () {
    return {
      model:{
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
      },
      serialNumberId:"",
      tradingAccount:[],
      tradingVariety:[],
      accountOption:[],
      varietyOption:[],
      openPlatformId:"",
      url: {
        accountList: "/gongqiang/accountPool/allList",
        varietyList: "/gongqiang/varietyPool/allList",
        queryById: "/gongqiang/userhomOrderPlatform/queryById",
        editAdd:"/gongqiang/userhomeOrderPlatform/editAdd",
        getBySerialNumberId:"/gongqiang/userhomeOrderPlatform/getBySerialNumberId"
      }
    }
  },
  computed: {
    formDisabled(){
      return this.disabled
    },
  },
  created () {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add () {
      this.edit(this.modelDefault);
    },
    edit (record) {
      this.serialNumberId=record.id;
      this.visible = true;
      this.loadData(record);
    },
    loadData (record) {
      let params={

      };
      getAction(this.url.accountList, params).then((res) => {
        debugger
        if (res.success) {
          this.accountOption=[];
          for (let i=0;i<res.result.length;i++)
          {
            let obj={};
            obj.key=res.result[i].accountName;
            obj.value=res.result[i].accountName;
            this.accountOption.push(obj);
          }
        }else{
          this.$message.warning(res.message)
        }
      }).finally(() => {
        this.loading = false
      })
      params={
        serialNumberId:record.id

      };
      getAction(this.url.getBySerialNumberId, params).then((res) => {
        if (res.success) {
          if(res.result!="") {
            this.tradingAccount = res.result.tradingAccount.split(',');
            this.tradingVariety = res.result.tradingVariety.split(',');
            this.openPlatformId = res.result.openPlatformId;
          }
        }else{
          this.$message.warning(res.message)
        }
      }).finally(() => {
        this.loading = false
      })


      getAction(this.url.varietyList, params).then((res) => {
        if (res.success) {
          this.varietyOption=[];
          for (let i=0;i<res.result.length;i++)
          {
            let obj={};
            obj.key=res.result[i].name
            obj.value=res.result[i].name
            this.varietyOption.push(obj);
          }
        }else{
          this.$message.warning(res.message)
        }
      }).finally(() => {
        this.loading = false
      })

    },

    submitForm () {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = this.url.editAdd;
          let method = '';
          method = 'post';
          let params=
            {
              serialNumberId:this.serialNumberId,
              tradingAccount:this.tradingAccount.join(','),
              tradingVariety:this.tradingVariety.join(','),
              openPlatformId:this.openPlatformId,
            }
          httpAction(httpurl,params,method).then((res)=>{
            if(res.success){
              that.$message.success(res.message);
              that.$emit('ok');
            }else{
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
  }
}
</script>